package org.jiamandan.com.strategy.listener;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jiamandan.com.constant.DXConstant;
import org.jiamandan.com.dto.DXBasicDto;
import org.jiamandan.com.strategy.IDXContextListener;
import org.springframework.stereotype.Service;


@Service(value = DXConstant.Service.CMD_004)
@AllArgsConstructor
@Slf4j
public class DX004GetIpPortListenerService implements IDXContextListener {
    @Override
    public void doExecute(DXBasicDto dxBasicDto, Channel ctx) throws Exception {
        log.info("接收嘉满电-DX获取004设备ip和端口指令(CMD=004)【"+dxBasicDto.getDeviceNo()+"】报文:" + JSON.toJSONString(dxBasicDto));
        String payload = dxBasicDto.getData();
        String ip1 = String.valueOf(Integer.parseInt(payload.substring(0, 2),16));
        String ip2 =  String.valueOf(Integer.parseInt(payload.substring(2, 4),16));
        String ip3 =  String.valueOf(Integer.parseInt(payload.substring(4, 6),16));
        String ip4 = String.valueOf(Integer.parseInt(payload.substring(6, 8),16));
        String port = String.valueOf(Integer.parseInt(payload.substring(8, 12),16));
        log.info("接收嘉满电-DX指令(CMD=004)，设备号=【{}】ip={}",dxBasicDto.getDeviceNo(),(ip1+"."+ip2+"."+ip3+"."+ip4+":"+port));
    }
}
